;
(function ($, window, document, undefined) {

    var pluginName = 'jqListScroll';

    function Plugin(element, options) {
        this.el = element;
        this.$el = $(element);
        this.options = $.extend({}, $.fn[pluginName].defaults, options);

        this.init();
    }

    Plugin.prototype = {

        init: function () {
            var _this = this;
            var timer = null;
            var scrollAnimate = null; //限制滚动过快
            //当滚动内容少时， box高度小于滚动内容高度，不滚动
            if (_this.$el.find("ul").height() -5 > _this.$el.height()){
                timer = setInterval(function () {
                    // if (_this.$el.find("ul").height() <= _this.$el.height()) {
                    //     clearInterval(timer);
                    // } else {
                    //     _this.marquee();
                    // }

                    _this.marquee();

                }, _this.options.speed);

                _this.$el.hover(function () {
                    clearInterval(timer);
                    if(_this.options.isMouse){
                        $(this).off("mousewheel");
                        $(this).on("mousewheel.scroll",function(event){
                            if(scrollAnimate){
                                clearTimeout(scrollAnimate);
                            }
                            scrollAnimate = setTimeout(function () {
                                var delta = event.originalEvent.deltaY;
                                var direction = delta<0?"up":"down";
                                if(direction == "up"){ // 鼠标上滚
                                    _this.marquee(-1,true);
                                }else{//  鼠标下滚
                                    _this.marquee(1,true);
                                }
                            },50);
                        })

                    }
                }, function () {
                    timer = setInterval(function () {
                        // if (_this.$el.find("ul").height() <= _this.$el.height()) {
                        //     clearInterval(timer);
                        // } else {
                        //     _this.marquee();
                        // }

                        _this.marquee();
                    }, _this.options.speed);
                });
            }
        },

        destroy: function () {
            this.$el.removeData();
        },
        /*@direction 滚动方向
        * @mouse 是否是鼠标触发的滚动
        * */
        marquee: function (direction,mouse) {
            var _this = this;
            var speed = 20; //一次滚动完成时间
            var direction = direction || 1;
            var scrollHeight = 1;    //初始化滚动，默认每次滚动1px,若type为2，则滚动rowHeight
            if(_this.options.type == 2 || mouse){
                scrollHeight = _this.options.rowHeight;
                speed = 300;
            }

            _this.$el.find("ul").animate({
                marginTop: '-='+direction*scrollHeight
            }, speed, function () {
                // var s = Math.abs(parseInt($(this).css("margin-top")));
                // if (s >= _this.options.rowHeight) {
                //     $(this).find("li").slice(0, 1).appendTo($(this));
                //     $(this).css("margin-top", 0);
                // }

                if(direction>0) { //页面上滚（鼠标下滚）
                    var s = Math.abs(parseInt($(this).css("margin-top")));
                    if(s >= _this.options.rowHeight){ // 向上滚，滚动超过一行
                        $(this).find("li").slice(0, 1).appendTo($(this));
                        $(this).css("margin-top", 0);
                    }
                } else {
                    var s = Math.abs(parseInt($(this).css("margin-top")));
                    if(s >= 0){ // 向下滚动（鼠标滚动），需要马上将最后一项移到最前面
                        $(this).find("li").slice(-1).prependTo($(this));
                        $(this).css("margin-top", 0);
                    }
                }
            });
        }

    };

    // 实现单例和下划线私有
    $.fn[pluginName] = function (options) {
        var args = arguments;

        if (options === undefined || typeof options === 'object') {
            return this.each(function () {
                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
                }
            });
        } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
            if (Array.prototype.slice.call(args, 1).length == 0 && $.inArray(options, $.fn[pluginName].getters) != -1) {
                // 返回值
                var instance = $.data(this[0], 'plugin_' + pluginName);
                return instance[options].apply(instance, Array.prototype.slice.call(args, 1));
            } else {
                return this.each(function () {
                    var instance = $.data(this, 'plugin_' + pluginName);
                    if (instance instanceof Plugin && typeof instance[options] === 'function') {
                        instance[options].apply(instance, Array.prototype.slice.call(args, 1));
                    }
                });
            }
        }
    };

    // 定义在数组中的方法,用来返回值,而不是实例
    $.fn[pluginName].getters = [];

    // 默认配置
    $.fn[pluginName].defaults = {
        speed: 40, // 滚动速度,值越大速度越慢
        rowHeight: 32, // 每行的高度
        isMouse:false,// 鼠标是否支持滚动
        type:1 //type:1 默认滚每次滚动1px,type:2 滚动为rowHeight
    };

})(jQuery, window, document);